package com.itqian.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itqian.common.exception.Assert;
import com.itqian.common.exception.ResponseEnum;
import com.itqian.common.utils.ResultVOUtil;
import com.itqian.common.vo.ResultVO;
import com.itqian.seller.entity.StoreInfo;
import com.itqian.seller.service.StoreInfoService;
import com.itqian.system.entity.Constants;
import com.itqian.system.mapper.RoleAuditMapper;
import com.itqian.system.service.RoleAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Map;

/**
 * @author shishuqian
 * date 2022/3/26
 * time 17:34
 **/
@RestController
@RequestMapping("/admin/ra")
public class RoleAuditController {

    @Autowired
    private RoleAuditService roleAuditService;

    @Autowired
    private RoleAuditMapper roleAuditMapper;

    @Autowired
    private StoreInfoService storeInfoService;
    /**
     *  分页查询审核数据
     *  type="wait"  未审核的
     *  type="passed" 审核通过的
     *  type=其它   审核未通过的
     */
    @GetMapping("/getAudit/{type}/{page}/{size}")
    public ResultVO getAudit(@PathVariable String type,@PathVariable Integer page,@PathVariable Integer size){
        //参数非空校验
        Assert.notBlank(type,ResponseEnum.TYPE_NO_SUPPORT);
        Assert.notNull(page, ResponseEnum.PAGE_NOTNULL);
        Assert.notNull(size,ResponseEnum.SIZE_NOTNULL);

        Map<String, Object> map = roleAuditService.getAudit(page, size, type);
        return ResultVOUtil.success(map);
    }

    /**
     *  审核通过
     *  id  审核id
     *  type 审核类型
     */
    @GetMapping("/pass/{id}/{type}")
    public ResultVO pass(@PathVariable Integer id,@PathVariable String type){
        //参数非空校验
        Assert.notNull(id,ResponseEnum.PARAM_NOTNULL);
        Assert.notBlank(type,ResponseEnum.PARAM_NOTNULL);
        //判断type是否合法
        HashSet<String> set = new HashSet<>();
        set.add("seller");set.add("runner");
        if (!set.contains(type)){
            return ResultVOUtil.fail("申请的身份类型不合法，请重试...");
        }

        /**
         *  1.审核通过修改用户身份
         *  2.判断申请身份，若是seller，还要给用户创建店铺(创建店铺时，要判断用户是否已有店铺，若有，则不用创建)
         */
        boolean pass = roleAuditService.pass(id, type);

        //如果审核商户身份，判断用户是否已有店铺(可能以前是seller，后来被取消了，但店铺信息会保留)
        if (type.equals(Constants.SELLER)){
            //查询进行审核的用户id
            Integer userId = roleAuditService.getById(id).getUserId();
            QueryWrapper<StoreInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("seller_id",userId);
            StoreInfo one = storeInfoService.getOne(wrapper);
            if (one == null){ //如果用户没有店铺
                boolean b = storeInfoService.addDefaultStore(userId);
            }
        }

        return ResultVOUtil.success(null);
    }

    /**
     *  审核不通过
     */
    @GetMapping("/reject/{id}")
    public ResultVO reject(@PathVariable Integer id){
        //参数非空校验
        Assert.notNull(id,ResponseEnum.PARAM_NOTNULL);

        boolean b = roleAuditMapper.reject(id);
        if (!b){
            return ResultVOUtil.fail("提交失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }


}
